package com.hxtec.polaris.controller;

import com.hxtec.polaris.commons.aop.Log;
import com.hxtec.polaris.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * @Describtion 分类相关
 * @Author yonyong
 * @Date 2019/11/11 14:45
 * @Version 1.0.0
 **/
@RestController
@RequestMapping(value = "category",produces = "application/json;charset=utf-8")
@CrossOrigin
@Api(value = "/category", tags = "分类相关")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    /**
     * 分类页面 获取所有分类数据
     * @return
     */
    @ApiOperation("获取商品分类列表")
    @GetMapping("getAll")
    public Object getAllCategory(){
        return categoryService.getAllCategory();
    }

    /**
     * 分类相关 添加分类节点
     * @return
     */
    @ApiOperation("添加商品分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pid", value = "父节点id", required = false, dataType = "String"),
            @ApiImplicitParam(name = "name", value = "添加的节点名", required = false, dataType = "String")
    })
    @PostMapping("add")
    @Log(value = "添加商品分类",comment = "管理员添加商品分类")
    public Object addCategory(HttpServletRequest request,String pid,String name,String img){
        return categoryService.addCategory(request,pid,name,img);
    }

    /**
     * 分类相关 删除分类节点
     * @return
     */
    @ApiOperation("删除商品分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids[]", value = "删除的节点集合", required = false,allowMultiple=true, dataType = "String")
    })
    @DeleteMapping("delete")
    @Log(value = "删除商品分类",comment = "管理员删除商品分类")
    public Object deleteCategory(HttpServletRequest request,String[] ids){
        List list = Arrays.asList(ids);
        return categoryService.deleteCategory(request,list);
    }

    /**
     * 分类相关 更新分类节点
     * @return
     */
    @ApiOperation("更新商品分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "编辑的节点id", required = false, dataType = "String"),
            @ApiImplicitParam(name = "pid", value = "当前节点编辑后的父节点id", required = false, dataType = "String"),
            @ApiImplicitParam(name = "name", value = "当前节点编辑后的节点名", required = false, dataType = "String"),
    })
    @PutMapping("update")
    @Log(value = "更新商品分类",comment = "管理员更新商品分类")
    public Object updateCategory(String id, String name,String img){
        return categoryService.updateCategory(id,name,img);
    }

    /**
     * 构建分类tree
     * @param id
     * @return
     */
    @GetMapping("buildTree")
    public Object buildTree(Integer id,String condition){
        return categoryService.buildTree(id);
    }

    /**
     * 搜索tree
     * @param condition
     * @return
     */
    @GetMapping("selectTree")
    public Object selectTree(String condition){
        return categoryService.selectTree(condition);
    }
}
